package com.siao.core
{
	import com.siao.data.Config;
	import com.siao.data.HashMap;
	import com.siao.errors.SingletonError;
	
	/**
	 * 配置中心
	 * @author SiaoLeon
	 */
	public class Configuration
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		private static var _instance:Configuration;
		
		/**
		 * 获取单例对象
		 */
		public static function get instance():Configuration
		{
			if (_instance == null)
			{
				_instance = new Configuration();
			}
			return _instance;
		}
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var configs:HashMap;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数，单例模式
		 */
		public function Configuration()
		{
			if (_instance != null)
			{
				throw new SingletonError(this);
			}
			_instance = this;
			this.init();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 保存XML配置
		 * @param xml XML配置
		 * @param name 配置名称，当与xml名称不同时，两者都将记录
		 * @return
		 */
		public function save(xml:XML, name:String=null):Config
		{
			var config:Config = new Config(xml);
			this.configs.put(config.name, config);
			
			if (name != config.name)
			{
				this.configs.put(name, config);
			}
			
			return config;
		}
		
		/**
		 * 获取配置文件
		 * @param name 配置名称
		 * @return
		 */
		public function getConfig(name:String):Config
		{
			return this.configs.getValue(name);
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 初始化
		 */
		private function init():void
		{
			this.configs = new HashMap();
		}
	
	}
}
